const http = require('http'),
    fs = require('fs');
var movieList = require('./data');

let routerTable = {
    '/': (res) => {
        let html = fs.readFileSync('./index.html').toString('utf8');
        res.end(html);
    },

    '/getlist': (res) => {
        var tmp = {};
        var list = [];
        for (var i = 0; i < movieList.length; i++) {
            if (movieList[i].movieRange !== '暂无评分') {
                list.push(movieList[i]);
            }
        }
        console.log(list);
        for (var i = 0; i < list.length; i++) {
            for (var j = 0; j < i; j++) {
                if (list[i].movieRange > list[j].movieRange) {
                    tmp = list[i];
                    list[i] = list[j];
                    list[j] = tmp;
                }
            }
        }
        console.log(list);
        res.end(JSON.stringify(list).toString('utf8'));

        // for (var i = 0; i < movieList.length; i++) {
        //     if (movieList[i].movieRange === '暂无评分') {
        //         movieList.splice(i, 1);
        //     }
        // }
        // console.log(movieList);
        // for (var i = 0; i < movieList.length; i++) {
        //     for (var j = 0; j < i; j++) {
        //         if (movieList[i].movieRange > movieList[j].movieRange) {
        //             tmp = movieList[i];
        //             movieList[i] = movieList[j];
        //             movieList[j] = tmp;
        //         }
        //     }
        // }
        // console.log(movieList);
        // res.end(JSON.stringify(movieList).toString('utf8'));
    }
}



http.createServer((req, res) => {
    if (!routerTable[req.url]) {
        res.statusCode = 404
        res.end('page not found')
        return
    }
    routerTable[req.url](res)
}).listen(8080)